/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package Giaodien;

import Lop.lGia;
import Lop.lNHOM_NEN;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import net.proteanit.sql.DbUtils;
import oracle.jdbc.OracleResultSet;

/**
 *
 * @author Administrator
 */
public class gBanggia extends javax.swing.JInternalFrame {

    /**
     * Creates new form gBanggia
     */
    
    private lNHOM_NEN BgNhomnen;
    private lGia t_gia;
    public gBanggia() {
        initComponents();
    }
    
    public gBanggia(lNHOM_NEN nhomnen) {
        initComponents();
        BgNhomnen = nhomnen;
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        tbBanggia = new javax.swing.JTable();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        CalNgay = new com.toedter.calendar.JDateChooser();
        spGia = new javax.swing.JSpinner();
        btCapnhat = new javax.swing.JButton();
        btXoa = new javax.swing.JButton();
        btThoat = new javax.swing.JButton();
        lbNhomnen = new javax.swing.JLabel();

        setTitle("Bang gia");
        addInternalFrameListener(new javax.swing.event.InternalFrameListener() {
            public void internalFrameActivated(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameClosed(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameClosing(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameDeactivated(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameDeiconified(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameIconified(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameOpened(javax.swing.event.InternalFrameEvent evt) {
                formInternalFrameOpened(evt);
            }
        });

        tbBanggia.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        tbBanggia.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Ngay", "Gia"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tbBanggia.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tbBanggiaMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(tbBanggia);

        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel1.setText("Ngày");

        jLabel2.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel2.setText("Giá");

        CalNgay.setDateFormatString("dd/MM/yyyy");
        CalNgay.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N

        spGia.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        spGia.setModel(new javax.swing.SpinnerNumberModel(1000000, 0, 100000000, 10000));

        btCapnhat.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        btCapnhat.setText("Cập nhật");
        btCapnhat.setPreferredSize(new java.awt.Dimension(90, 28));
        btCapnhat.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                btCapnhatMouseClicked(evt);
            }
        });

        btXoa.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        btXoa.setText("Xóa");
        btXoa.setPreferredSize(new java.awt.Dimension(90, 28));
        btXoa.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                btXoaMouseClicked(evt);
            }
        });

        btThoat.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        btThoat.setText("Thoát");
        btThoat.setPreferredSize(new java.awt.Dimension(90, 28));
        btThoat.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                btThoatMouseClicked(evt);
            }
        });

        lbNhomnen.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        lbNhomnen.setText("jLabel3");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 407, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(lbNhomnen, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(39, 39, 39)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(CalNgay, javax.swing.GroupLayout.PREFERRED_SIZE, 167, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 38, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(spGia, javax.swing.GroupLayout.PREFERRED_SIZE, 170, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(btCapnhat, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(btXoa, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(btThoat, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(lbNhomnen)
                .addGap(18, 18, 18)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 175, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1)
                            .addComponent(CalNgay, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel2)
                            .addComponent(spGia, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(btCapnhat, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btXoa, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btThoat, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(17, Short.MAX_VALUE))
        );

        setBounds(0, 0, 417, 383);
    }// </editor-fold>//GEN-END:initComponents

    private void btThoatMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btThoatMouseClicked
        // TODO add your handling code here:
        this.dispose();
    }//GEN-LAST:event_btThoatMouseClicked

    private void formInternalFrameOpened(javax.swing.event.InternalFrameEvent evt) {//GEN-FIRST:event_formInternalFrameOpened
        // TODO add your handling code here:
        try{
            this.lbNhomnen.setText("Khu : " + BgNhomnen.getRefKhutdc().getTenkhu() + 
                                    " / Nhom : " +  BgNhomnen.getTennhom());
            OracleResultSet rs;
            rs = BgNhomnen.getBanggia(fMain.cnn);
            tbBanggia.setModel(DbUtils.resultSetToTableModel(rs));
            this.CalNgay.setDate(new Date(System.currentTimeMillis()));
        }
        catch(SQLException | ClassNotFoundException e){
            JOptionPane.showMessageDialog(null, e);
        }
    }//GEN-LAST:event_formInternalFrameOpened

    private void tbBanggiaMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tbBanggiaMouseClicked
        // TODO add your handling code here:
        if(this.tbBanggia.getSelectedRow() >= 0){
            t_gia = new lGia();
            t_gia.setNgay((Date)this.tbBanggia.getModel().getValueAt(tbBanggia.getSelectedRow(), 0));
            java.math.BigDecimal bDec = (java.math.BigDecimal)this.tbBanggia.getModel().getValueAt(tbBanggia.getSelectedRow(), 1);
            t_gia.setGia(bDec.intValue());
            this.CalNgay.setDate(t_gia.getNgay());
            this.spGia.setValue(t_gia.getGia());
        }
    }//GEN-LAST:event_tbBanggiaMouseClicked

    private void btCapnhatMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btCapnhatMouseClicked
        try{
            // TODO add your handling code here:
            boolean isnew = true;
            int pos = 0;
            
            Date mydate = this.CalNgay.getDate();
            for(int i = 0; i < tbBanggia.getRowCount(); i++){
                Date ydate = (Date)tbBanggia.getModel().getValueAt(i, 0);
                if((mydate.getDate() == ydate.getDate()) && 
                        (mydate.getMonth()== ydate.getMonth()) &&
                        (mydate.getYear()== ydate.getYear())){
                    isnew = false;
                    pos = i;
                    break;
                }
            }
            
            if(isnew){
                t_gia = new lGia(this.CalNgay.getDate(), (int)this.spGia.getValue());
                ArrayList<lGia> myarr = new ArrayList();
                myarr.add(t_gia);
                BgNhomnen.setBanggia(myarr);
                if(!this.BgNhomnen.insertGia(fMain.cnn)) JOptionPane.showMessageDialog(null, "Khong the them du lieu!");
            }
            else{
                t_gia = new lGia((Date)this.tbBanggia.getModel().getValueAt(pos, 0), (int)this.spGia.getValue());
                ArrayList<lGia> myarr = new ArrayList();
                myarr.add(t_gia);
                BgNhomnen.setBanggia(myarr);
                if(!this.BgNhomnen.updateGia(fMain.cnn)) JOptionPane.showMessageDialog(null, "Khong the cap nhat du lieu!");
            }
            OracleResultSet rs;
            rs = BgNhomnen.getBanggia(fMain.cnn);
            tbBanggia.setModel(DbUtils.resultSetToTableModel(rs));
        }
        catch(Exception e){
            JOptionPane.showMessageDialog(null, e);
        }
    }//GEN-LAST:event_btCapnhatMouseClicked

    private void btXoaMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btXoaMouseClicked
        try{
            // TODO add your handling code here:
            boolean isexist = false;
            int pos = 0;
            Date mydate = this.CalNgay.getDate();
            for(int i = 0; i < tbBanggia.getRowCount(); i++){
                Date ydate = (Date)tbBanggia.getModel().getValueAt(i, 0);
                if((mydate.getDate() == ydate.getDate()) && 
                        (mydate.getMonth()== ydate.getMonth()) &&
                        (mydate.getYear()== ydate.getYear())){
                    isexist = true;
                    pos = i;
                    break;
                }
            }
            
            if(isexist){
                t_gia = new lGia((Date)this.tbBanggia.getModel().getValueAt(pos, 0), (int)this.spGia.getValue());
                ArrayList<lGia> myarr = new ArrayList();
                myarr.add(t_gia);
                BgNhomnen.setBanggia(myarr);
                if(!this.BgNhomnen.deleteGia(fMain.cnn)) JOptionPane.showMessageDialog(null, "Khong the xoa du lieu!");
            }
            else{
                JOptionPane.showMessageDialog(null, "Ngay " + this.CalNgay.getDate().toString() + " chu duoc thiet lap gia!");
            }
            OracleResultSet rs;
            rs = BgNhomnen.getBanggia(fMain.cnn);
            tbBanggia.setModel(DbUtils.resultSetToTableModel(rs));
        }
        catch(Exception e){
            JOptionPane.showMessageDialog(null, e);
        }
    }//GEN-LAST:event_btXoaMouseClicked


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private com.toedter.calendar.JDateChooser CalNgay;
    private javax.swing.JButton btCapnhat;
    private javax.swing.JButton btThoat;
    private javax.swing.JButton btXoa;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel lbNhomnen;
    private javax.swing.JSpinner spGia;
    private javax.swing.JTable tbBanggia;
    // End of variables declaration//GEN-END:variables
}
